package cn.edu.nuaa.aadl2.AdaGenerator.templateAda

import org.osate.aadl2.SubprogramCallSequence
import org.osate.aadl2.SubprogramCall
import org.osate.aadl2.Classifier

import cn.edu.nuaa.aadl2.AdaGenerator.utils.Tools
import cn.edu.nuaa.aadl2.AdaGenerator.templateAda.TemplateAda.*
import static extension cn.edu.nuaa.aadl2.AdaGenerator.templateAda.AnnexSubclauseTemplateAda.*
import static extension cn.edu.nuaa.aadl2.AdaGenerator.utils.StringUtils.*
import org.osate.aadl2.SubprogramImplementation
import org.osate.aadl2.SubprogramAccess
import org.osate.aadl2.AnnexSubclause
import org.osate.aadl2.DefaultAnnexSubclause

class SubprogramImplementationTemplateAda {
	static String fileName = TemplateAda.subprogramsFileName;
	static String initlineAds = "package "+fileName+ " is\n";
	static String initlineAdb = '''
		with Ada.Text_IO; use Ada.Text_IO;
		
		package body «fileName» is
		
	'''
	 
	def static create(SubprogramImplementation subprogramImplementation)'''
«««		«Tools.creatSubprogramFile(fileName.toLowerCase+".ads",initlineAds,subprogramImplementation.templateAds.toString)»
«««		«Tools.creatSubprogramFile(fileName.toLowerCase+".adb",initlineAdb,subprogramImplementation.templateAdb.toString)»
«««		«Tools.createFile("",fileName.toLowerCase+".ads",subprogramImplementation.templateAds.toString)»
«««		«Tools.createFile("Ada_codes",fileName.toLowerCase+".adb",subprogramImplementation.templateAdb.toString)»
		«println(subprogramImplementation.templateAds.toString)»
	'''
	
	def static templateAds(SubprogramImplementation subprogramImplementation)'''
	
		procedure «TemplateAda.packageName»_«subprogramImplementation.name.convertPoint»;
	'''
	
	def static templateAdb(SubprogramImplementation subprogramImplementation)'''

		procedure «TemplateAda.packageName»_«subprogramImplementation.name.convertPoint» is
		begin
			put_line("This is «subprogramImplementation.name.convertPoint»");
			«FOR SubprogramCall subprogramCall : subprogramImplementation.getSubprogramCalls»
				«TemplateAda.packageName»_«Tools.getCalledSubprogramName(subprogramCall.calledSubprogram.toString.convertPoint)»;
			«ENDFOR»
		«IF subprogramImplementation.ownedAnnexSubclauses.size > 0»
			«FOR AnnexSubclause annexSubclause : subprogramImplementation.ownedAnnexSubclauses»
				«genBehaviorAnnexVarible(annexSubclause as DefaultAnnexSubclause )»
				«genBehaviorAnnexState(annexSubclause as DefaultAnnexSubclause)»
				«genBehaviorAnnexTransition(annexSubclause as DefaultAnnexSubclause)»
				current_state : States;
			«ENDFOR»
		«ENDIF»
		end «TemplateAda.packageName»_«subprogramImplementation.name.convertPoint»;
		
	'''
}